package figure8.GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.midi.Instrument;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.sampled.Mixer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.Border;

import figure8.Controller;
import figure8.DrumMachineTrack;
import figure8.LoopTiming;
import figure8.Rec;

public class DrumMachineEditWindow extends JDialog {
	private int selectedItem;
	private LoopTiming timings;
	private Controller controller;
	private File[] drums;
	private int i, j ,x;
	private DrumMachineTrack drumMachine;
	
	public DrumMachineEditWindow (final Controller controller, File[] drumSamples, final DrumMachineTrack track, final DrumMachineSettingsPanel panel) {
		super();
		this.setLocation(controller.getWindow().getFrame().getX() + 80, controller.getWindow().getFrame()
				.getY() + 60);
		this.setPreferredSize(new Dimension(800, 700));
		this.drumMachine = track;
		this.controller = controller;
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		this.setAlwaysOnTop(true);
		this.timings = this.controller.getProject().getTiming();
		drums = drumSamples;
		GridLayout mainGrid = new GridLayout(drums.length, this.timings.getBeats() + 1);
		GridLayout beatGrid = new GridLayout(0, 4);
		JPanel main = new JPanel();
		main.setLayout(mainGrid);
		int k = 0;
		for (i = 0; i < drums.length; i++) {
			for (j = 0; j < this.timings.getBeats() + 1; j++) {
				if (j == 0) main.add(new JLabel(drums[i].getName()));
				else {
					JPanel beat = new JPanel();
					beat.setLayout(beatGrid);
					for (x = 0; x < 4; x++) {
						final int j2 = j;
						final int x2 = x;
						final int i2 = i;
						
						String s;
						
						if (x == 0) {
							char c = Character.forDigit(j, 10);
							s = c + "";
						} else {
							s = " ";
						}
						
						JToggleButton button = new JToggleButton(s);
						switch (k){
							case 0: {
								button.setForeground(Color.magenta);
								break;
							}
							case 1: {
								button.setForeground(Color.blue);
								break;
							}
							case 2: {
								button.setForeground(Color.black);
								break;
							}
							case 3: {
								button.setForeground(Color.orange);
								break;
							}
							case 4: {
								button.setForeground(Color.pink);
								break;
							}
							case 5: {
								button.setForeground(Color.yellow);
								break;
							}
							case 6: {
								button.setForeground(Color.gray);
								break;
							}
							case 7: {
								button.setForeground(Color.green);
								break;
							}
							case 8: {
								button.setForeground(Color.cyan);
								button.setBackground(Color.cyan);
								break;
							}
							case 9: {
								button.setForeground(Color.red);
								break;
							}
						}
						
						button.setPreferredSize(new Dimension(50, 100));
						
						if(drumMachine.isPressed((4 * (j2 - 1)) + x2, i2)) {
							button.doClick();
						}
						button.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								drumMachine.drumChange((4 * (j2 - 1)) + x2, i2);
								panel.changeGraph();
								track.fireTrackChangedEvent(true);
							}
						});
						beat.add(button);

					}
					main.add(beat);
					
				}
				
			}
			k++;
		}
	    this.add(main);
	    main.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		this.pack();
		this.setVisible(true);
	}
	
	
	public void buttonPress() {
		drumMachine.drumChange((4 * (j - 1)) + x, i);
	}
}